<?php
/*==================================================================================
	TODO: Old database code below. Must get rid of these. Just keeping for
	compatibility for now
====================================================================================*/
//session_start();

$dbhost = "localhost";
$dbname = "speedtest";
$dbuser = "root";
$dbpass = "";

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());


/**
 * Database connection class
 * @author chathuraw
 *
 */
class DBConnection {
	private $db;
	
	/* db connection properties = EDIT HERE */
    private $dbhost = "localhost";
    private $dbname = "speedtest";
    private $dbuser = "root";
    private $dbpwd = "";

	private $query_counter = 0; // statistics
	private $total_time_expended = 0;
	
	/**
	 * Class constructor. Initializes database connection.
	 * @param boolean $conn
	 */
	public function __construct($conn = true) {
		if ($conn) {
			$this->connect();
	    }
	}

	/**
	 * Get database connection 
	 */
	function connect() {
		$dsn = "mysql:host=" . $this->dbhost . ";dbname=" . $this->dbname;
	    try {
			$this->db = new PDO($dsn, $this->dbuser, $this->dbpwd);
		
		} catch (PDOException $e) {
			print "Database connection failed: " . $e->getMessage() . "<br/>";
		    die();    
		}
	}

	/**
	 * Executes a query with parameters
	 * @param string $sql
	 * @param array $params
	 * @return Ambiguous <unknown, PDOStatement>
	 */
	function query($sql, $params) {		
		$this->query_counter++;
		$start_time = microtime();
		$statement = $this->db->prepare($sql);
	    $result = $statement->execute($params);
		$this->total_time_expended += (microtime() - $start_time);
		return $result ? $statement : $result;
	}

	/**
	 * Query counter for statistics
	 * @return number
	 */
	function get_querycount() {
		return $this->query_counter;   
	}

	/**
	 * Get execution time for statistics
	 * @return number
	 */
	function get_exec_time() {
		return $this->total_time_expended;
	}
}

?>